/**
 * Created by colynn on 16/8/16.
 */
/*
document.getElementById('view-list').onclick = function () {
    document.getElementById('main-view-list').style.display = 'block';
}*/

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

var csrftoken = getCookie('csrftoken');

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});


$(document).ready(
    $("#index_add_host").click(
        function () {
            window.location.href = "/add_host/";
        }
    ),

    $("#host_name").change(
        function () {
            var host_name = $("#host_name").val();
            var patt = /^[\w-]+$/;

            if (!patt.test(host_name)) {
                alert('请输入你正确的host name');
                return false;
            }
            $.ajax({
                url: "/verify_host/",
                type: "post",
                dataType: "json",
                data: {
                    host_name: host_name
                }

            }).done(function (msg) {
                if (msg == 1) {
                    $("#alert").html('该主机已经存在于帐户下, 可至首页更新该主机信息!');
                    $("#alert").addClass("alert alert-warning");
                    $("#add_host").prop('disabled',true);  //disabled add_host button.
                }
                else {
                    $("#alert").removeClass("alert alert-warning");
                    $("#alert").html('');
                    $("#add_host").prop('disabled',false);
                }
            })
        }
    ),

    $("#services_text").mousemove(
        function () {
             $("#installed_service_info").stop().show();
        }
    ),

    $("#services_text").mouseout(
        function () {
             $("#installed_service_info").stop().hide(300);
        }
    ),

    $("#add_host").click(
        function () {
            var host_name = $("#host_name").val();
            var ip = $("#ip").val();
            var port = $("#port").val();
            var enable_lvm = $("#check_enable_lvm").is(':checked');
            var service = $("#services_text").val();

            var patt = /^[\w-]+$/;

            if (!patt.test(host_name)) {
                alert('请输入你正确的 HostName');
                return false;
            }

            patt = /^\d+\.\d+\.\d+\.\d+$/;
            if (!patt.test(ip)) {
                alert('你输入的ip不合法');
                return false;
            }
            patt = /^\d+$/;
            if (!patt.test(port)) {
                alert('你输入的port 不合法!');
                return false;
            }

            $.post("/add_host/",
                {
                    host_name: host_name,
                    ip: ip,
                    port: port,
                    en_lvm : enable_lvm,
                    service: service
                },
                function (data, status) {
                    if (status == "success" && data == 0) {
                        window.location.href="/";
                    } else {
                        //alert("failed");
                    }
                });
        }
    ),

    $("#update_host").click(
        function () {
            var host_name = $("#host_name_key").val();
            var ip = $("#ip").val();
            var port = $("#port").val();
            var enable_lvm = $("#check_enable_lvm").is(':checked');
            var services_name = $("#services_text").val();
            $.ajax({
                url: "/update_host/",
                type: "post",
                dataType: "json",
                data: {
                    _id: host_name,
                    ip: ip,
                    port: port,
                    server_lvm: enable_lvm,
                    service: services_name
                }

            }).done(function ( msg) {
                if (msg.basic_update == 1 && msg.service_update ) {
                    //console.log(msg);
                    window.location.href="/";
                }
            })
        }
    ),
    
    // 侦听 input 输入字段的改变
    $("input.inputField").change(function() {
        var inputField = this;
        var inputName = $(inputField).attr('name');
        // console.log(inputName, $(inputField).val());
        $.ajax({
                url: '/update_service/',
                type: 'post',
                dataType: 'json',
                data: {
                    para_key: inputName,
                    para_val: $(inputField).val()
                }
        }).done(function( msg ) {
            if (msg == 1) {
                console.log(" update " + inputName + " parameter succeed.");
            }
            else {
                console.log(" update failed, return: " + msg);
            }
        });

    }),

    // refresh the page
    $("#tab_1-refresh").click(
        function () {
            window.location.reload();
        }
    ),

    // preview page
    $("#preview").click(
        function () {
            window.location.href = "/preview/"
        }

    ),


    $("#run_history").click(
        function () {
            window.location.href = "/run_history/"
        }

    ),

    // run ansible page, need review input username and password, then click run.
    $("#setup_service").click(
        function () {
            window.location.href = "/review_run/"
        }

    ),

    $('.tab-line-icon').click(
        function () {
            var entry_id = $(this).attr('id');
            var content_id = '#' + entry_id + '_content';
            $(content_id).toggle();
            if ($(content_id).css('display') == 'none'){
                $('#' + entry_id).removeClass('fa-angle-down');
                $('#' + entry_id).addClass('fa-angle-up');
            }else {
                $('#' + entry_id).removeClass('fa-angle-up');
                $('#' + entry_id).addClass('fa-angle-down');
            }

        }
    )
)


/*
document.getElementById('setup_services').onclick = function () {
    console.log(document.getElementsByClassName('preview-content')[0]);
}

document.getElementById('preview').onclick = function () {
    window.location.href = "/preview"
}

document.getElementById('setup_now').onclick = function () {
    window.location.href = "/run_ansible"
}
*/
